package first.group.ssh.packet;

import first.group.ssh.SSHConsts;
import first.group.ssh.packet.reader.IntegerReader;
import java.nio.ByteBuffer;

public class ChannelWindowAdjustPacket extends Packet{
    public final int recipientChannel;
    public final int bytesToAdd;

    public ChannelWindowAdjustPacket(int recipientChannel, int bytesToAdd) {
        super(SSHConsts.SSH_MSG_CHANNEL_WINDOW_ADJUST);
        this.recipientChannel = recipientChannel;
        this.bytesToAdd = bytesToAdd;
    }
    public static PacketParser<ChannelWindowAdjustPacket> PARSER = new ChannelWindowAdjustPacket.Parser();

    private static class Parser extends PacketParser<ChannelWindowAdjustPacket> {

        @Override
        public ChannelWindowAdjustPacket parse(ByteBuffer buffer) {
            var recipientChannel = IntegerReader.readInteger(buffer);
            if (recipientChannel == null) {
                return null;
            }
            var bytesToAdd = IntegerReader.readInteger(buffer);
            if (bytesToAdd == null) {
                return null;
            }
            return new ChannelWindowAdjustPacket(recipientChannel, bytesToAdd);
        }
    }
    @Override
    public void write(ByteBuffer buffer) {
        buffer.putInt(recipientChannel);
        buffer.putInt(bytesToAdd);
    }
}
